﻿using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TMPro;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;

namespace UIFramwork
{
    [System.Serializable]
    public class UIFramworkSetting : ScriptableObject
    {
        private static UIFramworkSetting singleton;

        public static UIFramworkSetting Singleton
        {
            get
            {
                if (singleton == null)
                {
                    UIFramworkSetting.singleton = Resources.Load<UIFramworkSetting>("UI Setting");
                }
                return singleton;
            }
        }



        public static string Version => "0.0.1";

        public static string PathDetail = "/Scripts/UI/UIDetail/";
        public static string PathTemplate = "/@Framwork/UI Framwork @王鑫伟/Editor/Templete/";
        public static Component BlackList;


        [MenuItem("UI 框架/程序化模版 #&C", false, 1)]
        static void DoCheckUI()
        {
            Transform target = Selection.activeTransform;

            if (target == null)
            {
                Debug.LogError("未选中对象");
                return;
            }

            bool isGet = target.TryGetComponent(out RectTransform rt);
            if (!isGet)
            {
                Debug.LogError("对象不属于UI分类");
                return;
            }

            Debug.Log("开始索引");
            StringBuilder build = new();

            foreach (Transform child in target)
            {
                DoIndex(child, string.Empty, ref build);
            }

        }
        static void DoIndex(Transform root, string path , ref StringBuilder build)
        {
            if (TryGetMonos(root, out Component[] monos))
            {
                foreach (Component comp in monos) 
                {
                    string name = DoCheck(comp);
                    string indexSTR = $"\0\0\0\0\0\0\0\0{name} = transform.Find(\"\").GetComponent<{comp.GetType().Name}>()";

                    build.AppendLine(indexSTR);
                    Debug.Log(indexSTR);
                }
            }

            path = string.IsNullOrEmpty(path) ? $"{root.name}" : $"{path}/{root.name}";
            Debug.Log(path);
            if (root.childCount > 0 && !root.gameObject.name.StartsWith("!"))
            {
                foreach (Transform item in root)
                { 
                    DoIndex(item, path, ref build);
                }
            }
        }
        static void DoCreate(Transform root, StringBuilder componentsLib)
        {
            string fileName = $"Detail{root.gameObject.name}.cs";

        }
        static string DoCheck(Component component)
        {
            string name = component.gameObject.name;

            if (component is Button) return $"{name}Btn";
            if (component is Image) return $"{name}Img";
            if (component is Toggle) return $"{name}Tgl";
            if (component is TextMeshProUGUI) return $"{name}TMP";
            if (component is InputField) return $"{name}InputField";
            if (component is TMP_InputField) return $"{name}InputField";

            return name;
        }
        static bool TryGetMonos(Transform node, out Component[] monos)
        {
            monos = null;

            if (node.gameObject.name.StartsWith("!")) return false;

            monos = node.GetComponents<Component>().Where(component => !(component is RectTransform || component is Renderer)).ToArray();
            return monos.Length != 0;
        }
    }
}
